package me.hunche.app.web.page;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import me.hunche.app.dao.ActivityDao;
import me.hunche.app.dao.CarDao;
import me.hunche.app.dao.UserDao;
import me.hunche.app.domain.Activity;
import me.hunche.app.domain.Car;
import me.hunche.app.domain.User;
import me.hunche.app.service.ActivityService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hp.hpl.helper.misc.DateHelper;

//@Controller
//@RequestMapping(value = "/manage")
public class ManageController {
	
	@Autowired private UserDao userDao;
	@Autowired private CarDao carDao;
	@Autowired private ActivityService activityService;
	
	@Autowired private ActivityDao activityDao;
	
	@RequestMapping(value="user/add")
	 public @ResponseBody User addUser(@RequestParam(value="name") String name){
		User user = new User();
		user.setNickname(name);
		userDao.save(user);
		
		Car c = new Car();
		c.setLicenseNo("A08M97");
		c.setOwner(user);
		c.setOwnerId(user.getId());
		carDao.save(c);
		
		c.setOwner(null);
		user.setCar(c);
		return user;
	}
	

	@RequestMapping(value="user/all")
	public @ResponseBody List<User> getAllUser(){
		List<User> l = userDao.getAll();
		return l;
	}
	
	
	@RequestMapping(value="activity/create")
	 public @ResponseBody Activity createActivity() throws ParseException{
		
		String sdate = "2013-05-25";
		String line = "加州-冉家坝-花卉园";
		String rendezvous = "江北加州童心桥";
		String sdatetime = "2013-05-25 07:00:00"; 
		
		Date d = new SimpleDateFormat(DateHelper.YYYY_MM_DD).parse(sdate);
		Date dt = new SimpleDateFormat(DateHelper.YYYY_MM_DD_HH_MM_SS).parse(sdatetime);
		Activity a = activityService.createActivity(d, line, rendezvous, dt);
		
		return a;
	}

	
	@RequestMapping(value="activity/all")
	public @ResponseBody List<Activity> getAllActivity(){
		List<Activity> l = activityDao.getAll();
		return l;
	}
}
